﻿#include "FITKAbaqusAdaptorStep.h"
#include "InpKeyLineSpliter.h"
#include "FITKAbaqusINPReader.h"
#include "FITKAbaqusINPWriter.h"
#include "FITKAbaqusAdaptorStepDynamic.h"
#include "FITKAbaqusAdaptorStepStatic.h"
#include "FITKAbaqusAdaptorStepFrequency.h"
#include "FITKAbaqusAdaptorLoad.h"
#include "FITKAbaqusAdaptorBoundary.h"
#include "FITKAbaqusAdaptorOutput.h"
#include "FITKAbaqusAdaptorInteraction.h"
#include "FITKAbaqusAdaptorPredefinedField.h"
#include "FITK_Kernel/FITKCore/FITKVec3D.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceModel/FITKAssInstanceTR.h"
#include "FITK_Interface/FITKInterfaceModel/FITKComponentManager.h"
#include "FITK_Interface/FITKInterfaceModel/FITKUnstructuredMesh.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAssembly.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMeshPartVTK.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaStepManager.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaStepInit.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaStepFrequency.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaStepGeneralStatic.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaStepDynamicExplicit.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKBoundaryLocation.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaLoadManager.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaBoundaryConditionManager.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAbstractLoad.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaLoadConcentratedForce.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaLoadPressure.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaLoadMoment.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaLoadBodyForce.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaLoadGravity.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAbstractBoundaryCondition.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaBoundaryConditionDisplacement.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaBoundaryConditionSymmetry.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaBoundaryConditionVelocity.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaFieldOutput.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaHistoryOutput.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaFieldOutputManager.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaHistoryOutputManager.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInteractionSurfToSurfExplicit.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInteractionGeneralContactExplicit.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInteractionManager.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaPredefinedFieldTemperature.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaPredefinedFieldVelocity.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaPredefinedFieldManager.h"

#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"

//命名空间
namespace IO
{
    QString FITKAbaqusAdaptorStep::getAdaptorClass()
    {
        //返回命名空间+类名
        return "IO::FITKAbaqusAdaptorStep";
    }

    void FITKAbaqusAdaptorStep::setCaseData(AbaqusData::FITKDataCase* caseData)
    {
        if (caseData == nullptr) return;
        _caseData = caseData;
        _stepDataManager = _caseData->getStepManager();
        _fieldOutputManager = _caseData->getFieldOutputManager();
        _historyOutputManager = _caseData->getHistoryOutputManager();
    }


    bool FITKAbaqusAdaptorStep::adaptR()
    {
        auto stepData = dynamic_cast<Interface::FITKAbaAbstractStep*>(_dataObj);
        if (!_stepDataManager || !_reader || !stepData || !_fieldOutputManager || !_historyOutputManager) return false;
        QString line;
        //回退至step关键字
        while (_reader->getCurrentLineNum() > 1)
        {
            _reader->backLine();
            line = _reader->getCurrentLine();
            line.remove('"');
            if (line.toLower().startsWith("*step"))
            {
                //文件读取机制 需要
                _reader->readLine();
                break;
            }
        }
        //如果回退到起始都没找到关键字则异常退出
        if (_reader->getCurrentLineNum() == 1) return false;
        //分析步的描述
        QString description;
        while (!_reader->atEnd())
        {
            QString str = _reader->readLine();
            if (str.startsWith("*"))
            {
                _reader->backLine();
                break;
            }
            description += str;
        }
        //关键字处理 文件中两行*可以确定类型 中间行可能有注释
        InpKeyLineSpliter s(line, _reader);
        QString stepName = s["name"];
        QString nlgeom = s["nlgeom"];
        //暂不考虑该参数
        QString inc = s["inc"];

        //定义大类型 暂时无用
        bool perturbation = s.hasKey("perturbation");
        //设置分析步的描述
        stepData->setDataObjectName(stepName);
        stepData->setNlgeom(nlgeom.count() == 3);
        if (!inc.isEmpty())
        {
            //_stepData->setMaximumNumberOfIncrements(inc.toDouble());
        }
        stepData->setDescription(description);

        //单行的一定要执行continue 不然会死循环。
        while (!_reader->atEnd())
        {
            //判断分析步类型
            bool readOK = false;
            line = _reader->readLine();
            if (!line.startsWith("*") || line.startsWith("**")) continue;
            InpKeyLineSpliter s(line, _reader);
            //发送计算进度信号
            _reader->sendCurrentPercent();
            QString sH = s.getHeader().toLower();
            if (sH == "static")
            {
                readOK = this->readStatic();
            }
            else if (sH == "dynamic")
            {
                readOK = this->readDynamic();
            }
            else if (sH == "frequency")
            {
                readOK = this->readFrequency();
            }
            else if (sH == "cload" || sH == "dsload" || sH == "dload")
            {
                readOK = this->readLoad();
            }
            else if (sH == "boundary")
            {
                readOK = this->readBoundary();
            }
            else if (sH == "contact" || sH == "contact inclusions" || sH == "contact pair")
            {
                readOK = this->readInteraction();
            }
            else if (sH == "restart")
            {
                readOK = this->readRestart();
                continue;
            }
            else if (sH == "output" || sH == "element output" || sH == "node output" || sH == "contact output")
            {
                readOK = this->readOutput();
            }
            else if (sH == "temperature")
            {
                readOK = this->readPredefinedField();
            }
            else if (sH == "end step")
            {
                this->updateOutputDeactiveStepR();
                return true;
            }

            if (readOK)
            {
                _reader->backLine();
            }
            else
            {
                //读取失败返回错误
                //return false;
            }
        }
        return false;
    }
    bool FITKAbaqusAdaptorStep::adaptW()
    {
        auto stepData = dynamic_cast<Interface::FITKAbaAbstractStep*>(_dataObj);
        if (!_stepDataManager || !_writer || !stepData || !_fieldOutputManager || !_historyOutputManager) return false;
        auto stepType = stepData->getStepType();
        bool isWrite = true;
        // 写出Step函数主体
        QTextStream* stream = _writer->getStream();
        QString stepName = stepData->getDataObjectName();
        stepName = stepName.contains(' ') ? QString("\"%1\"").arg(stepName) : stepName;
        *stream << "*Step";
        *stream << ", " << QString("name=%1").arg(stepName);
        QString nlgeom = stepData->getNlgeom() ? "YES" : "NO";
        *stream << ", " << QString("nlgeom=%1").arg(nlgeom);
        //写出perturbation关键字
        if (stepData->isPerturbation())
        {
            *stream << ", perturbation";
        }
        *stream << endl;

        //写出可能的描述
        QString decription = stepData->getDecription();
        if (!decription.isEmpty())
        {
            *stream << decription << endl;
        }

        //写出step具体类型
        if (stepType == Interface::FITKAbaAbstractStep::FITKAbaStepType::DynamicExplicit)
        {
            isWrite &= writeDynamic();
        }
        else if (stepType == Interface::FITKAbaAbstractStep::FITKAbaStepType::GeneralStatic)
        {
            isWrite &= writeStatic();
        }
        else if (stepType == Interface::FITKAbaAbstractStep::FITKAbaStepType::Frequency)
        {
            isWrite &= writeFrequency();
        }
        else if (stepType == Interface::FITKAbaAbstractStep::FITKAbaStepType::Init)
        {
            isWrite &= true;
        }
        //写出load
        isWrite &= writeBoundary();
        isWrite &= writeLoad();
        isWrite &= writePredefinedField();
        isWrite &= writeInteraction();
        isWrite &= writeRestart();
        isWrite &= writeOutput();

        return isWrite;
    }
    bool FITKAbaqusAdaptorStep::readInit()
    {
        return true;
    }

    bool FITKAbaqusAdaptorStep::readStatic()
    {
        _stepData = dynamic_cast<Interface::FITKAbaStepGeneralStatic*>(_dataObj);
        if (!_stepDataManager || !_reader || !_stepData) return false;
        //回退一行 验证读取正确性
        _reader->backLine();
        QString line = _reader->readLine();
        if (!line.toLower().startsWith("*static")) return false;

        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorStepStatic>("INP", _stepData);
        if (adaptor == nullptr) return false;

        //设置读取器
        adaptor->setFileReader(_reader);
        adaptor->setDataObject(_stepData);
        bool ok = adaptor->adaptR();

        delete adaptor;
        return ok;
    }
    bool FITKAbaqusAdaptorStep::readDynamic()
    {
        _stepData = dynamic_cast<Interface::FITKAbaStepDynamicExplicit*>(_dataObj);
        if (!_stepDataManager || !_reader || !_stepData) return false;
        //回退一行 验证读取正确性
        _reader->backLine();
        QString line = _reader->readLine();
        if (!line.toLower().startsWith("*dynamic")) return false;

        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorStepDynamic>("INP", _stepData);
        if (adaptor == nullptr) return false;

        //设置读取器
        adaptor->setFileReader(_reader);
        adaptor->setDataObject(_stepData);
        bool ok = adaptor->adaptR();

        delete adaptor;
        return ok;
    }

    bool FITKAbaqusAdaptorStep::readFrequency()
    {
        _stepData = dynamic_cast<Interface::FITKAbaStepFrequency*>(_dataObj);

        if (!_stepDataManager || !_reader) return false;
        //回退一行 验证读取正确性
        _reader->backLine();
        QString line = _reader->readLine();
        if (!line.toLower().startsWith("*frequency")) return false;

        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorStepFrequency>("INP", _stepData);
        if (adaptor == nullptr) return false;

        //设置读取器
        adaptor->setFileReader(_reader);
        adaptor->setDataObject(_stepData);
        bool ok = adaptor->adaptR();

        delete adaptor;
        return ok;
    }

    bool FITKAbaqusAdaptorStep::readRestart()
    {
        auto stepData = dynamic_cast<Interface::FITKAbaAbstractStep*>(_dataObj);
        if (!_stepDataManager || !_reader || !stepData || !_caseData) return false;
        //回退一行 验证读取正确性
        _reader->backLine();
        QString line = _reader->readLine();
        InpKeyLineSpliter s(line, _reader);
        bool ok = false;
        //重启动数据将在分析中被写入
        if (s.hasKey("write"))
        {
            //覆盖
            if (s.hasKey("overlay"))
                stepData->setOverlayState(true);
            //time marks
            if (s["time marks"].toLower() == "yes")
                stepData->setTimeMarkState(true);
            else if (s["time marks"].toLower() == "no")
                stepData->setTimeMarkState(false);
            //输出间隔
            QString interval = s["number interval"].toLower().trimmed();
            stepData->setRestartWriteIntervals(interval.toInt(&ok));
            if (!ok)return false;
            //输出频率
            QString frequency = s["frequency"].toLower().trimmed();
            stepData->setRestartWritFrequency(frequency.toInt(&ok));
            if (!ok)return false;
        }

        return true;
    }

    bool FITKAbaqusAdaptorStep::readOutput()
    {
        if (!_stepDataManager || !_reader || !_stepData || !_caseData) return false;
        //回退一行 验证读取正确性
        _reader->backLine();
        QString line = _reader->readLine();
        InpKeyLineSpliter s(line, _reader);
        QString sH = s.getHeader().toLower();

        //互斥参数的最后一个 其余两个为field、history
        QString diagnostics = s["diagnostics"];
        //共同的可选参数
        QString matName = s["name"];
        //Standard下的可选参数
        QString frequency = s["frequency"];
        QString modeList = s["mode list"];
        QString numberInterval = s["number interval"];
        QString timeMarks = s["time marks"];
        QString timePoints = s["time points"];

        //TODO 抑制输出 不用读取
        if (frequency == "0")
        {
            _reader->readLine();
            return true;
        }

        //校验 可能不需要
        if (sH != "element output" && sH != "node output" && sH != "contact output" && sH != "output") return false;
        //关键字处理
        Interface::FITKAbaAbstractOutput* dataObject{};
        if (s.hasKey("field"))
        {
            dataObject = new Interface::FITKAbaFieldOutput();
        }
        else if (s.hasKey("history"))
        {
            dataObject = new Interface::FITKAbaHistoryOutput();
            //return false;
        }
        else if (s.getHeader().contains("Output") && _previousData != nullptr)
        {
            if (_previousData->getOutputType() == Interface::FITKAbaAbstractOutput::FITKAbaOutputType::Field)
            {
                dataObject = new Interface::FITKAbaFieldOutput();
            }
            else if (_previousData->getOutputType() == Interface::FITKAbaAbstractOutput::FITKAbaOutputType::History)
            {
                dataObject = new Interface::FITKAbaHistoryOutput();
            }

            if (dataObject == nullptr) return false;
            if (!dataObject->copy(_previousData))
            {
                delete dataObject;
                return false;
            }
            dataObject->clearVariables();
        }
        if (dataObject == nullptr) return false;

        //设置分析步ID
        dataObject->setCreatedStep(_stepData->getDataObjectID());
        //添加到管理器 并设置名称
        if (dataObject->getOutputType() == Interface::FITKAbaAbstractOutput::FITKAbaOutputType::Field)
        {
            _fieldOutputManager->appendDataObj(dataObject);
            int index = _fieldOutputManager->getDataCount();
            dataObject->setDataObjectName(QString("F-Output-" + QString::number(index)));
        }
        else if (dataObject->getOutputType() == Interface::FITKAbaAbstractOutput::FITKAbaOutputType::History)
        {
            _historyOutputManager->appendDataObj(dataObject);
            int index = _historyOutputManager->getDataCount();
            dataObject->setDataObjectName(QString("H-Output-" + QString::number(index)));
        }

        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorOutput>("INP", "Interface::FITKAbaAbstractOutput");
        if (adaptor == nullptr)
        {
            delete dataObject;
            return false;
        }
        //设置读取器
        adaptor->setFileReader(_reader);
        adaptor->setCaseData(_caseData);
        adaptor->setStepData(_stepData);
        adaptor->setDataObject(dataObject);
        bool ok = adaptor->adaptR();
        delete adaptor;
        if (!ok)
        {
            delete dataObject;
        }
        else
        {
            //保存上一个output的对象，用于区分类型以及copy
            _previousData = dataObject;
        }
        return ok;
    }

    bool FITKAbaqusAdaptorStep::readBoundary()
    {
        if (!_caseData || !_reader) return false;
        //回退一行 验证读取正确性
        _reader->backLine();
        QString lineKey = _reader->readLine();
        InpKeyLineSpliter s(lineKey, _reader);
        QString type = s["type"].toLower();
        if (s["op"].toLower() == "new")
            this->setBoundaryDeactiveStepR();
        do {
            QString line = _reader->readLine();
            if (line.startsWith("*"))
            {
                break;
            }
            QStringList str = line.split(',');
            if (str.size() < 2)return false;
            int value = str.at(1).toInt();
            int loadNum = _caseData->getBCManager()->getDataCount();
            Interface::FITKAbaAbstractBoundaryCondition* boundary = nullptr;
            if ((type.isEmpty() || type == "displacement") && str.size() == 2)
            {
                boundary = new Interface::FITKAbaBoundaryConditionSymmetry;
            }
            else if ((type.isEmpty() || type == "displacement") && value > 0 && value <= 6)
            {
                boundary = new Interface::FITKAbaBoundaryConditionDisplacement;
            }
            else if (type == "velocity" && value > 0 && value <= 6)
            {
                boundary = new Interface::FITKAbaBoundaryConditionVelocity;
            }
            if (boundary == nullptr)return false;
            boundary->setDataObjectName(QString("Boundary_%1").arg(loadNum + 1));
            boundary->setCreatedStep(_dataObj->getDataObjectID());
            //通过工厂创建
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorBoundary>("INP", boundary);
            if (adaptor == nullptr)
            {
                _reader->consoleMessage(3, QString("Create AdaptorBoundary Error. File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
                delete boundary;
                return false;
            }
            adaptor->setFileReader(_reader);
            adaptor->setDataObject(boundary);
            adaptor->setCaseData(_caseData);
            adaptor->setLineKeywords(lineKey);
            bool ok = adaptor->adaptR();
            //读取完毕释放读取器
            delete adaptor;
            if (!ok)
            {
                _reader->consoleMessage(3, QString("Read Boundary Error : %1, File:%2, Line:%3").arg(boundary->getDataObjectName()).arg(__FILE__).arg(__LINE__));
                delete boundary;
                return false;
            }
            _reader->consoleMessage(1, "Read Boundary : " + boundary->getDataObjectName());
            auto manager = _caseData->getBCManager();
            manager->appendDataObj(boundary);
        } while (!_reader->atEnd());

        return true;
    }

    bool FITKAbaqusAdaptorStep::readInteraction()
    {
        auto stepData = dynamic_cast<Interface::FITKAbaAbstractStep*>(_dataObj);
        //管理器获取
        Interface::FITKAbaInteractionManager* _interactionManager = _caseData->getInteractionManager();
        if (_interactionManager == nullptr)return false;
        if (!_stepDataManager || !_reader || !stepData || !_caseData) return false;
        //回退一行 验证读取正确性
        _reader->backLine();
        //关键字处理
        Interface::FITKAbaAbstractInteraction* dataObject{};
        QString line = _reader->readLine();
        QString nLine = _reader->getCurrentLine();
        InpKeyLineSpliter s(line, _reader);
        QString sH = s.getHeader().toLower();
        QString op = s["op"];
        QString cpset = s["cpset"];

        //如果读到了非激活状态的对象则跳出
        if (op == "DELETE" && !cpset.isEmpty())
        {
            _reader->readLine();
            this->setInteractionDeactiveStepR(1, stepData->getDataObjectID(), cpset);
            return true;
        }
        else if (op == "NEW" && !nLine.contains("*Contact"))
        {
            _reader->readLine();
            this->setInteractionDeactiveStepR(2, stepData->getDataObjectID(), "");
            return true;
        }

        if (sH == "contact pair")
        {
            dataObject = new Interface::FITKAbaInteractionSurfToSurfExplicit();
            //设置名字
            int interactionIndex = _interactionManager->getDataCount();
            dataObject->setDataObjectName(cpset + "-" + QString::number(interactionIndex));
        }
        else if (sH == "contact" || sH == "contact inclusions")
        {
            dataObject = new Interface::FITKAbaInteractionGeneralContactExplicit();
            //设置名字
            int stepIndex = _stepDataManager->getDataIndex(_stepData);
            dataObject->setDataObjectName("general_contact_" + QString::number(stepIndex));
        }

        if (dataObject == nullptr)
        {
            _writer->consoleMessage(3, QString("Create Interaction Object Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            return false;
        }
        _interactionManager->appendDataObj(dataObject);
        //设置stepID
        dataObject->setCreatedStep(stepData->getDataObjectID());

        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorInteraction>("INP", dataObject);
        if (adaptor == nullptr)
        {
            delete dataObject;
            return false;
        }
        //设置读取器
        adaptor->setFileReader(_reader);
        adaptor->setCaseData(_caseData);
        adaptor->setStepData(stepData);
        adaptor->setDataObject(dataObject);
        bool ok = adaptor->adaptR();
        delete adaptor;
        if (!ok)
        {
            _writer->consoleMessage(3, QString("Read Interaction Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            delete dataObject;
        }
        return ok;
    }

    bool FITKAbaqusAdaptorStep::readLoad()
    {
        if (!_stepDataManager || !_reader || !_dataObj) return false;
        //创建工具
        FITKAbaqusAdaptorLoad loadTool;
        loadTool.setCaseData(_caseData);
        //回退一行 验证读取正确性
        _reader->backLine();
        QString lineKey = _reader->readLine();
        InpKeyLineSpliter s(lineKey, _reader);
        QString sH = s.getHeader().toLower();
        bool isOp = false;
        //设置取消激活状态
        if (s["op"].toLower() == "new")
        {
            loadTool.setLoadDeactiveStepR(sH, _dataObj->getDataObjectID());
            isOp = true;
        }
        do {
            QString line = _reader->readLine();
            if (line.startsWith("*"))
                break;
            QStringList str = line.split(',');
            if (str.size() < 3)return false;
            QString typeStr = str.at(1).trimmed().toLower();
            int value = typeStr.toInt();
            //通过类型创建load
            Interface::FITKAbaAbstractLoad* load = nullptr;
            if (sH == "cload" && value > 0 && value <= 3)
                load = new Interface::FITKAbaLoadConcentratedForce();
            else if (sH == "cload" && value > 3 && value <= 6)
                load = new Interface::FITKAbaLoadMoment();
            else if (sH == "dsload" && typeStr == "p")
                load = new Interface::FITKAbaLoadPressure();
            else if (sH == "dload" && typeStr == "grav")
                load = new Interface::FITKAbaLoadGravity();
            else if (sH == "dload" && typeStr.size() == 2 && typeStr.at(0) == 'b')
                load = new Interface::FITKAbaLoadBodyForce();
            else if (sH == "dload" && typeStr.contains("p"))
                load = new Interface::FITKAbaLoadPressure();
            if (load == nullptr)return false;
            //设置创建的stepId
            load->setCreatedStep(_dataObj->getDataObjectID());
            //通过工厂创建
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorLoad>("INP", load);
            if (adaptor == nullptr)
            {
                _reader->consoleMessage(3, QString("Create AdaptorLoad Error. File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
                delete load;
                return false;
            }
            adaptor->setFileReader(_reader);
            adaptor->setDataObject(load);
            adaptor->setCaseData(_caseData);
            adaptor->setLineKeywords(lineKey);
            bool ok = adaptor->adaptR();
            bool isValueEmp = adaptor->isValueEmpty();
            //读取完毕释放读取器
            delete adaptor;
            //数据是否为空
            if (isValueEmp)
            {
                delete load;
                continue;
            }
            if (!ok)
            {
                _reader->consoleMessage(3, QString("Read Load Error : %1, File:%2, Line:%3").arg(load->getDataObjectName()).arg(__FILE__).arg(__LINE__));
                delete load;
                return false;
            }
            _reader->consoleMessage(1, "Read Load : " + load->getDataObjectName());
            auto loadManager = _caseData->getLoadManager();
            loadManager->appendDataObj(load);
        } while (!_reader->atEnd());

        return true;
    }

    bool FITKAbaqusAdaptorStep::readPredefinedField()
    {
        if (!_reader || !_caseData || !_dataObj) return false;
        //创建工具
        FITKAbaqusAdaptorPredefinedField predefinedTool;
        predefinedTool.setCaseData(_caseData);
        //回退一行 验证读取正确性
        _reader->backLine();
        QString lineKeyWord = _reader->readLine();
        InpKeyLineSpliter s(lineKeyWord, _reader);
        QString sH = s.getHeader().toLower();
        //获取预定义管理器
        Interface::FITKAbaPredefinedFieldManager* predefinedFieldManager = _caseData->getPredefinedFieldManager();
        if (!predefinedFieldManager)return false;
        //设置取消激活状态
        if (s["op"].toLower() == "new")
            predefinedTool.setPredefinedFieldDeactiveStepR(sH, _dataObj->getDataObjectID());
        do {
            QString line = _reader->readLine();
            if (line.startsWith("*"))
                break;
            QString name;
            //创建预定义场
            Interface::FITKAbaAbstractPredefinedField* fieldData = nullptr;
            if (sH == "temperature")
            {
                fieldData = new Interface::FITKAbaPredefinedFieldTemperature;
                name = "Field";
            }
            if (!fieldData)return false;
            //设置名称和设置创建stepId
            fieldData->setDataObjectName(QString("%1-%2").arg(name).arg(predefinedFieldManager->getDataCount() + 1));
            fieldData->setCreatedStep(_dataObj->getDataObjectID());
            //通过工厂创建
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorPredefinedField>("INP", fieldData);
            if (adaptor == nullptr)
            {
                _reader->consoleMessage(3, QString("Create PredefinedField Error. File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
                delete fieldData;
                return false;
            }
            adaptor->setFileReader(_reader);
            adaptor->setDataObject(fieldData);
            adaptor->setCaseData(_caseData);
            adaptor->setLineKeywords(lineKeyWord);
            bool ok = adaptor->adaptR();
            //读取完毕释放读取器
            delete adaptor;
            if (!ok)
            {
                _reader->consoleMessage(3, QString("Read PredefinedField Error : %1, File:%2, Line:%3").arg(fieldData->getDataObjectName()).arg(__FILE__).arg(__LINE__));
                delete fieldData;
                return false;
            }
            _reader->consoleMessage(1, "Read PredefinedField : " + fieldData->getDataObjectName());
            predefinedFieldManager->appendDataObj(fieldData);
        } while (!_reader->atEnd());
        return true;
    }

    bool FITKAbaqusAdaptorStep::writeStatic()
    {
        _stepData = dynamic_cast<Interface::FITKAbaStepGeneralStatic*>(_dataObj);
        if (!_stepDataManager || !_writer || !_stepData) return false;
        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorStepStatic>("INP", _stepData);
        if (adaptor == nullptr) return false;

        //设置写出器
        adaptor->setFileWriter(_writer);
        adaptor->setDataObject(_stepData);
        bool ok = adaptor->adaptW();

        delete adaptor;
        return ok;
    }
    bool FITKAbaqusAdaptorStep::writeDynamic()
    {
        _stepData = dynamic_cast<Interface::FITKAbaStepDynamicExplicit*>(_dataObj);
        if (!_stepDataManager || !_writer || !_stepData) return false;
        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorStepDynamic>("INP", _stepData);
        if (adaptor == nullptr) return false;

        //设置写出器
        adaptor->setFileWriter(_writer);
        adaptor->setDataObject(_stepData);
        bool ok = adaptor->adaptW();

        delete adaptor;
        return ok;
    }

    bool FITKAbaqusAdaptorStep::writeFrequency()
    {
        _stepData = dynamic_cast<Interface::FITKAbaStepFrequency*>(_dataObj);
        if (!_stepDataManager || !_writer || !_stepData) return false;
        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorStepFrequency>("INP", _stepData);
        if (adaptor == nullptr) return false;

        //设置写出器
        adaptor->setFileWriter(_writer);
        adaptor->setDataObject(_stepData);
        bool ok = adaptor->adaptW();

        delete adaptor;
        return ok;
    }

    bool FITKAbaqusAdaptorStep::writeOutput()
    {
        bool ok = true;
        QTextStream* stream = _writer->getStream();

        QList<Interface::FITKAbaAbstractOutput*> currentStepOutput;
        //后续改为排序
        for (int i = 0; i < _fieldOutputManager->getDataCount(); ++i)
        {
            auto output = _fieldOutputManager->getDataByIndex(i);
            currentStepOutput.append(output);
        }

        for (int i = 0; i < _historyOutputManager->getDataCount(); ++i)
        {
            auto output = _historyOutputManager->getDataByIndex(i);
            currentStepOutput.append(output);
        }

        for (int i = 0; i < currentStepOutput.size(); ++i)
        {
            auto output = currentStepOutput[i];

            int stepID = _stepData->getDataObjectID();
            //判断每个输出在当前step下的状态
            auto state = output->getStateInStep(stepID);
            bool isWrite = false;
            isWrite |= state == Interface::FITKAbaStepEnum::StateInStep::Created;
            isWrite |= state == Interface::FITKAbaStepEnum::StateInStep::Propagated;
            isWrite |= state == Interface::FITKAbaStepEnum::StateInStep::Modified;
            if (!isWrite) continue;

            *stream << "**" << endl;
            if (output->getOutputType() == Interface::FITKAbaAbstractOutput::FITKAbaOutputType::Field)
            {
                *stream << "** FIELD OUTPUT: " << output->getDataObjectName() << endl;
            }
            else if (output->getOutputType() == Interface::FITKAbaAbstractOutput::FITKAbaOutputType::History)
            {
                *stream << "** HISTORY OUTPUT: " << output->getDataObjectName() << endl;
            }
            *stream << "**" << endl;

            //检测
            //if (_previousData != nullptr)
            //    output->setObjectName(_previousData->objectName());
            //通过工厂创建
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorOutput>("INP", "Interface::FITKAbaAbstractOutput");
            if (adaptor == nullptr) return false;
            _previousData = output;
            //设置写出器
            adaptor->setFileWriter(_writer);
            adaptor->setCaseData(_caseData);
            adaptor->setDataObject(output);
            ok = adaptor->adaptW();
            delete adaptor;
            if (!ok)
            {
                _writer->consoleMessage(3, QString("Write OUTPUT Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            }
        }
        return ok;
    }

    bool FITKAbaqusAdaptorStep::writeBoundary()
    {
        if (!_writer || !_caseData)return false;
        //获取管理器
        auto boundaryManager = _caseData->getBCManager();
        int count = boundaryManager->getDataCount();
        QTextStream* stream = _writer->getStream();
        //用于判断是否写出标题
        bool okTitle = false;
        for (int i = 0; i < count; ++i)
        {
            auto boundary = dynamic_cast<Interface::FITKAbaAbstractBoundaryCondition*>(boundaryManager->getDataByIndex(i));
            if (!boundary)continue;
            //用于判断边界是否激活
            if (!boundary->getActiveState())continue;
            QString op;
            int stepId = this->writeBoundaryDataType(boundary, op);
            if (stepId == 0)continue;
            if (!okTitle)
            {
                okTitle = true;
                *stream << "**" << endl;
                *stream << "** BOUNDARY CONDITIONS" << endl;
                *stream << "**" << endl;
            }
            //通过工厂创建
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorBoundary>("INP", boundary);
            if (adaptor == nullptr) return false;
            adaptor->setFileWriter(_writer);
            adaptor->setCaseData(_caseData);
            adaptor->setDataObject(boundary);
            adaptor->setWriterData(op, stepId);
            bool ok = adaptor->adaptW();
            delete adaptor;
            if (!ok)
                _writer->consoleMessage(3, QString("write Boundary Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            if (!ok) return false;
        }
        return true;
    }

    bool FITKAbaqusAdaptorStep::writeInteraction()
    {
        auto stepData = dynamic_cast<Interface::FITKAbaAbstractStep*>(_dataObj);
        //获取管理器
        Interface::FITKAbaInteractionManager* _interactionManager = _caseData->getInteractionManager();
        if (!_writer || !_caseData || !_interactionManager || !stepData)return false;
        QList<Interface::FITKAbaAbstractInteraction*> currentInteraction;
        QList<Interface::FITKAbaAbstractInteraction*> currentInteractionSurf;
        QList<Interface::FITKAbaAbstractInteraction*> currentInteractionGeneral;
        //用于判断第一个元素是否为面面相互作用
        bool isOneSurf = false;
        QTextStream* stream = _writer->getStream();
        int stepID = _stepData->getDataObjectID();

        //排序
        for (int i = 0; i < _interactionManager->getDataCount(); ++i)
        {
            auto interaction = _interactionManager->getDataByIndex(i);

            if (interaction->getCreatedStep() == -1) continue;

            if (interaction->getInteractionType() == Interface::FITKAbaAbstractInteraction::FITKAbaInteractionType::SurfToSurfContactExplicit)
            {
                if (i == 0)isOneSurf = true;
                currentInteractionSurf.append(interaction);
            }
            else if (interaction->getInteractionType() == Interface::FITKAbaAbstractInteraction::FITKAbaInteractionType::GeneralContactExplicit)
            {
                currentInteractionGeneral.append(interaction);
            }
        }

        if (isOneSurf)
        {
            currentInteraction.append(currentInteractionSurf);
            currentInteraction.append(currentInteractionGeneral);
        }
        else
        {
            currentInteraction.append(currentInteractionGeneral);
            currentInteraction.append(currentInteractionSurf);
        }
        //用于判断是否写出标题
        if (currentInteraction.size() > 0)
        {
            *stream << "**" << endl;
            *stream << "** INTERACTIONS" << endl;
            *stream << "**" << endl;
        }
        //写出
        for (int i = 0; i < currentInteraction.size(); ++i)
        {
            auto interaction = currentInteraction[i];
            if (!interaction) continue;
            //用于判断是否激活
            if (!interaction->getActiveState())continue;

            bool isAdd = false;
            //如果step1是传递，并且创建的是init可以写出。  interaction createStep应该是不对的
            if (_stepDataManager->getDataIndex(stepData) == 1
                && interaction->getStateInStep(stepID) == Interface::FITKAbaStepEnum::StateInStep::Propagated
                && interaction->getInteractionType() != Interface::FITKAbaAbstractInteraction::FITKAbaInteractionType::GeneralContactExplicit)
            {
                isAdd = true;
            }

            isAdd |= interaction->getStateInStep(stepID) == Interface::FITKAbaStepEnum::StateInStep::Created;  //创建
            isAdd |= interaction->getStateInStep(stepID) == Interface::FITKAbaStepEnum::StateInStep::Modified; //修改
            isAdd |= interaction->getStateInStep(stepID) == Interface::FITKAbaStepEnum::StateInStep::Deactived;//已停用
            if (!isAdd) continue;
            //QString op;
            //int stepId = this->writeBoundaryDataType(interaction, op);
            //if (stepId == 0)continue;

            *stream << QString("** Interaction: %1").arg(interaction->getDataObjectName()) << endl;
            //通过工厂创建
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorInteraction>("INP", interaction);
            if (adaptor == nullptr) return false;
            adaptor->setFileWriter(_writer);
            adaptor->setCaseData(_caseData);
            adaptor->setStepData(stepData);
            adaptor->setDataObject(interaction);
            bool ok = adaptor->adaptW();
            delete adaptor;
            if (!ok)
            {
                _writer->consoleMessage(3, QString("write Interaction Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
                return false;
            }
        }
        return true;
    }

    bool FITKAbaqusAdaptorStep::writeLoad()
    {
        if (!_writer || !_caseData || !_dataObj)return false;
        //获取管理器
        auto loadManaget = _caseData->getLoadManager();
        int count = loadManaget->getDataCount();
        QTextStream* stream = _writer->getStream();
        bool okTitle = false;
        for (int i = 0; i < count; ++i)
        {
            auto load = dynamic_cast<Interface::FITKAbaAbstractLoad*>(loadManaget->getDataByIndex(i));
            if (!load)continue;
            //用于判断载荷是否激活
            if (!load->getActiveState())continue;
            QString op;
            int stepId = this->writeLoadDataType(load, op);
            if (stepId == 0)continue;
            if (!okTitle)
            {
                okTitle = true;
                *stream << "**" << endl;
                *stream << "** LOADS" << endl;
                *stream << "**" << endl;
            }
            //通过工厂创建
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorLoad>("INP", load);
            if (adaptor == nullptr) return false;
            adaptor->setFileWriter(_writer);
            adaptor->setCaseData(_caseData);
            adaptor->setDataObject(load);
            adaptor->setWriterData(op, stepId);
            bool ok = adaptor->adaptW();
            delete adaptor;
            if (!ok)
                _writer->consoleMessage(3, QString("write Load Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            if (!ok) return false;
        }
        return true;
    }

    bool FITKAbaqusAdaptorStep::writePredefinedField()
    {
        if (!_writer || !_caseData || !_dataObj)return false;
        //获取预定义管理器
        Interface::FITKAbaPredefinedFieldManager* predefinedFieldManager = _caseData->getPredefinedFieldManager();
        if (!predefinedFieldManager)return false;
        int count = predefinedFieldManager->getDataCount();
        QTextStream* stream = _writer->getStream();
        bool okTitle = false;
        for (int i = 0; i < count; ++i)
        {
            Interface::FITKAbaAbstractPredefinedField* fieldData = predefinedFieldManager->getDataByIndex(i);
            if (!fieldData)continue;
            //用于判断载荷是否激活
            if (!fieldData->getActiveState())continue;
            QString op;
            int stepId = this->writePredefinedFieldDataType(fieldData, op);
            if (stepId == 0)continue;
            if (!okTitle)
            {
                okTitle = true;
                *stream << "**" << endl;
                *stream << "** PREDEFINED FIELDS" << endl;
                *stream << "**" << endl;
            }
            //通过工厂创建
            auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorPredefinedField>("INP", fieldData);
            if (adaptor == nullptr) return false;
            adaptor->setFileWriter(_writer);
            adaptor->setCaseData(_caseData);
            adaptor->setDataObject(fieldData);
            adaptor->setWriterData(op, stepId);
            bool ok = adaptor->adaptW();
            delete adaptor;
            if (!ok)
                _writer->consoleMessage(3, QString("write PredefinedField Error,File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
            if (!ok) return false;
        }
        return true;
    }

    bool FITKAbaqusAdaptorStep::writeRestart()
    {
        auto stepData = dynamic_cast<Interface::FITKAbaAbstractStep*>(_dataObj);
        if (!_writer || !_caseData || !_dataObj || !stepData)return false;
        QTextStream* stream = _writer->getStream();
        *stream << "**" << endl;
        *stream << "** OUTPUT REQUESTS" << endl;
        *stream << "**" << endl;
        //判断step是否存在显式
        Interface::FITKAbaAbstractStep::FITKAbaStepType type = stepData->getStepType();
        bool isExplicit = false;
        if (type == Interface::FITKAbaAbstractStep::FITKAbaStepType::DynamicExplicit)
            isExplicit = true;
        //写出关键字
        QString lineOne = "*Restart, write";
        //覆盖
        if (stepData->getOverlayState())
            lineOne += ", overlay";
        if (isExplicit || stepData->getRestartWriteIntervals() != 0)
        {
            //输出间隔
            lineOne += QString(", number interval=%1").arg(stepData->getRestartWriteIntervals());
            //time marks
            if (stepData->getTimeMarkState())
                lineOne += QString(", time marks=YES");
            else
                lineOne += QString(", time marks=NO");
        }
        else
        {
            //输出频率
            lineOne += QString(", frequency=%1").arg(stepData->getRestartWriteFrequency());
        }
        *stream << lineOne << endl;
        return true;
    }

    void FITKAbaqusAdaptorStep::setBoundaryDeactiveStepR()
    {
        //获取管理器
        auto bcManager = _caseData->getBCManager();
        if (!bcManager)return;
        int count = bcManager->getDataCount();
        //边界设置取消激活的分析步Id
        for (int i = 0; i < count; ++i)
        {
            Interface::FITKAbaAbstractBoundaryCondition* boundary = dynamic_cast<Interface::FITKAbaAbstractBoundaryCondition*>(bcManager->getDataByIndex(i));
            if (!boundary || boundary->getCreatedStep() == _dataObj->getDataObjectID() || boundary->getDeactiveStep() != -1) continue;
            boundary->setDeactiveStep(_dataObj->getDataObjectID());
        }
    }

    void FITKAbaqusAdaptorStep::setInteractionDeactiveStepR(int type, int stepID, QString name)
    {
        //获取相互作用管理器
        auto interactionManager = _caseData->getInteractionManager();

        for (int i = 0; i < interactionManager->getDataCount(); ++i)
        {
            Interface::FITKAbaAbstractInteraction* interaction{};
            if (type == 1) //面面接触
            {
                interaction = dynamic_cast<Interface::FITKAbaInteractionSurfToSurfExplicit*>(interactionManager->getDataByIndex(i));
                if (!interaction || interaction->getDataObjectName() != name) continue;
                interaction->setDeactiveStep(stepID);
            }
            else if (type == 2) //通用接触
            {
                interaction = dynamic_cast<Interface::FITKAbaInteractionGeneralContactExplicit*>(interactionManager->getDataByIndex(i));
                if (!interaction || interaction->getCreatedStep() == stepID || interaction->getDeactiveStep() != -1) continue;
                interaction->setDeactiveStep(stepID);
            }
        }
    }

    void FITKAbaqusAdaptorStep::updateOutputDeactiveStepR()
    {
        //获取相互作用管理器
        auto outputFiledManager = _caseData->getFieldOutputManager();
        auto outputHistoryManager = _caseData->getHistoryOutputManager();

        for (int i = 0; i < outputFiledManager->getDataCount(); ++i)
        {
            auto field = dynamic_cast<Interface::FITKAbaFieldOutput*>(outputFiledManager->getDataByIndex(i));
            if (!field || field->getCreatedStep() == _stepData->getDataObjectID() || field->getDeactiveStep() != -1) continue;
            field->setDeactiveStep(_stepData->getDataObjectID());
        }

        for (int i = 0; i < outputHistoryManager->getDataCount(); ++i)
        {
            auto history = dynamic_cast<Interface::FITKAbaHistoryOutput*>(outputHistoryManager->getDataByIndex(i));
            if (!history || history->getCreatedStep() == _stepData->getDataObjectID() || history->getDeactiveStep() != -1) continue;
            history->setDeactiveStep(_stepData->getDataObjectID());
        }
    }

    int FITKAbaqusAdaptorStep::writeBoundaryDataType(Interface::FITKAbaAbstractBoundaryCondition* boundary, QString &op)
    {
        op = ", op=NEW";
        //step数据存储器
        if (!_dataObj || !boundary)return 0;
        int currentStepId = _dataObj->getDataObjectID();
        //取消激活
        int deactiveStepId = boundary->getDeactiveStep();
        if (deactiveStepId == currentStepId)
            return -1;
        //获取boundary管理器
        auto bcManager = _caseData->getBCManager();
        if (!bcManager)return 0;
        //获取上一级stepId
        Interface::FITKAbaStepManager* stepManager = _caseData->getStepManager();
        if (!stepManager)return 0;
        int index = stepManager->getDataIndex(stepManager->getDataByID(currentStepId)) - 1;
        int superiorStep = stepManager->getDataByIndex(index)->getDataObjectID();
        //获取case下的所有boundary
        int count = bcManager->getDataCount();
        //用于获取是否在本step中有取消激活的边界
        bool isExistDeactive = false;
        //用于记录当前的step中是否有数据与上一个step的激活状态不一致
        bool isDataEnable = true;
        for (int i = 0; i < count; ++i)
        {
            Interface::FITKAbaAbstractBoundaryCondition* data = dynamic_cast<Interface::FITKAbaAbstractBoundaryCondition*>(bcManager->getDataByIndex(i));
            if (data->getDeactiveStep() == currentStepId)
                isExistDeactive = true;
            if (data->getBoundaryConditionType() == Interface::FITKAbaAbstractBoundaryCondition::FITKAbaBoundaryConditionType::DisplacementRotation)
            {
                Interface::FITKAbaBoundaryConditionDisplacement* boundaryData = dynamic_cast<Interface::FITKAbaBoundaryConditionDisplacement*>(data);
                if (boundaryData->getU1State(currentStepId) != boundaryData->getU1State(superiorStep) || boundaryData->getU2State(currentStepId) != boundaryData->getU2State(superiorStep) || boundaryData->getU3State(currentStepId) != boundaryData->getU3State(superiorStep)
                    || boundaryData->getUR1State(currentStepId) != boundaryData->getUR1State(superiorStep) || boundaryData->getUR2State(currentStepId) != boundaryData->getUR2State(superiorStep) || boundaryData->getUR3State(currentStepId) != boundaryData->getUR3State(superiorStep))
                    isDataEnable = false;
            }
        }
        //创建
        int createStepId = boundary->getCreatedStep();
        if (createStepId == currentStepId)
        {
            if (!isExistDeactive && isDataEnable)
                op.clear();
            return createStepId;
        }
        //是否修改
        if (boundary->isModifiedInStep(currentStepId))
        {
            if (!isExistDeactive && isDataEnable)
                op.clear();
            return currentStepId;
        }
        //获取step管理器下创建、当前、取消激活这三个状态的index
        index = stepManager->getDataIndex(_dataObj);
        int createIndex = stepManager->getDataIndex(stepManager->getDataByID(createStepId));
        int deactiveIndex = stepManager->getDataCount();
        if (deactiveStepId != -1)
            deactiveIndex = stepManager->getDataIndex(stepManager->getDataByID(deactiveStepId));
        if (index < 0 || createIndex < 0 || deactiveIndex < 0)return 0;
        //当前step是否传递
        if (!(createIndex < index && deactiveIndex > index))return 0;
        //获取写出的stepId
        int stepId = 0;
        for (int i = index; i > createIndex; --i)
        {
            auto step = stepManager->getDataByIndex(i);
            if (!step)continue;
            if (!boundary->isModifiedInStep(step->getDataObjectID()))continue;
            stepId = step->getDataObjectID();
            break;
        }
        if (stepId == 0)
            stepId = createStepId;
        //传递且有取消激活
        if (isExistDeactive || !isDataEnable)
            return stepId;
        return 0;
    }

    int FITKAbaqusAdaptorStep::writeLoadDataType(Interface::FITKAbaAbstractLoad* load, QString &op)
    {
        op = ", op=NEW";
        //step数据存储器
        if (!_dataObj || !load)return 0;
        int currentStepId = _dataObj->getDataObjectID();
        //取消激活
        int deactiveStepId = load->getDeactiveStep();
        if (deactiveStepId == currentStepId)
            return -1;
        //获取load类型和集合或表面id
        Interface::FITKAbaAbstractLoad::FITKAbaLoadType type = load->getLoadType();
        //通过类型获取输出关键字一致的类型
        QList<Interface::FITKAbaAbstractLoad::FITKAbaLoadType> typeList;
        if (type == Interface::FITKAbaAbstractLoad::FITKAbaLoadType::ConcentratedForce || 
            type == Interface::FITKAbaAbstractLoad::FITKAbaLoadType::Moment)
            typeList << Interface::FITKAbaAbstractLoad::FITKAbaLoadType::ConcentratedForce 
            << Interface::FITKAbaAbstractLoad::FITKAbaLoadType::Moment;
        else if (type == Interface::FITKAbaAbstractLoad::FITKAbaLoadType::Pressure)
            typeList << Interface::FITKAbaAbstractLoad::FITKAbaLoadType::Pressure;
        else if (type == Interface::FITKAbaAbstractLoad::FITKAbaLoadType::BodyForce || 
            type == Interface::FITKAbaAbstractLoad::FITKAbaLoadType::Gravity)
            typeList << Interface::FITKAbaAbstractLoad::FITKAbaLoadType::BodyForce 
            << Interface::FITKAbaAbstractLoad::FITKAbaLoadType::Gravity;
        if (typeList.isEmpty())return 0;
        //获取load管理器
        auto loadManager = _caseData->getLoadManager();
        if (!loadManager)return 0;
        //获取case下的所有load
        int count = loadManager->getDataCount();
        //用于获取集合是否是唯一的
        bool isRepeat = true;
        //用于获取同类型下的所有setid
        QList<int> setIDList;
        //用于获取是否在本step中是否有取消激活的载荷
        bool isExistDeactive = false;
        for (int i = 0; i < count; ++i)
        {
            Interface::FITKAbaAbstractLoad* data = dynamic_cast<Interface::FITKAbaAbstractLoad*>(loadManager->getDataByIndex(i));
            Interface::FITKAbaAbstractLoad::FITKAbaLoadType typeData = data->getLoadType();
            if (!typeList.contains(typeData))continue;
            if (setIDList.contains(data->getComponentID()))
                isRepeat = false;
            else
                setIDList.append(data->getComponentID());
            if (data->getDeactiveStep() == currentStepId)
                isExistDeactive = true;
        }
        //创建
        int createStepId = load->getCreatedStep();
        if (createStepId == currentStepId)
        {
            if (isRepeat && !isExistDeactive)
                op.clear();
            return createStepId;
        }
        //step管理
        auto stepManager = _caseData->getStepManager();
        if (!stepManager)return 0;
        //是否修改//??
        if (load->isModifiedInStep(currentStepId))
        {
            if (isRepeat && !isExistDeactive)
                op.clear();
            return currentStepId;
        }
        //获取step管理器下创建、当前、取消激活这三个状态的index
        int index = stepManager->getDataIndex(_dataObj);
        int createIndex = stepManager->getDataIndex(stepManager->getDataByID(createStepId));
        int deactiveIndex = stepManager->getDataCount() - 1;
        if (deactiveStepId != -1)
            deactiveIndex = stepManager->getDataIndex(stepManager->getDataByID(deactiveStepId));
        if (index < 0 || createIndex < 0 || deactiveIndex < 0)return 0;
        //当前step是否传递
        if (createIndex > index && deactiveIndex < index)return 0;
        //获取写出的stepId
        int stepId = 0;
        for (int i = index; i > createIndex; --i)
        {
            auto step = stepManager->getDataByIndex(i);
            if (!step)continue;
            if (!load->isModifiedInStep(step->getDataObjectID()))continue;
            stepId = step->getDataObjectID();
            break;
        }
        if (stepId == 0)
            stepId = createStepId;
        //具有数量大于2或传递且有取消激活
        if (!isRepeat || isExistDeactive)
            return stepId;
        return 0;
    }

    int FITKAbaqusAdaptorStep::writePredefinedFieldDataType(Interface::FITKAbaAbstractPredefinedField* fieldData, QString &op)
    {
        op = ", op=NEW";
        //step数据存储器
        if (!_dataObj || !fieldData)return 0;
        int currentStepId = _dataObj->getDataObjectID();
        Interface::FITKAbaStepEnum::StateInStep stateType = fieldData->getStateInStep(currentStepId);
        if (stateType == Interface::FITKAbaStepEnum::StateInStep::Deactived || stateType == Interface::FITKAbaStepEnum::StateInStep::ResetToInitial)
        {
            return -1;
        }
        if (!(stateType == Interface::FITKAbaStepEnum::StateInStep::Created || stateType == Interface::FITKAbaStepEnum::StateInStep::Modified)) return 0;
        //获取预定义管理器
        Interface::FITKAbaPredefinedFieldManager* predefinedFieldManager = _caseData->getPredefinedFieldManager();
        if (!predefinedFieldManager)return 0;
        // 用于获取是否在本step中是否有取消激活的预定义
        bool isExistDeactive = false;
        //获取数量
        int count = predefinedFieldManager->getDataCount();
        for (int i = 0; i < count; ++i)
        {
            Interface::FITKAbaAbstractPredefinedField* data = predefinedFieldManager->getDataByIndex(i);
            if (!data || fieldData->getPreDefinedType() != data->getPreDefinedType())continue;
            if (data->getStateInStep(currentStepId) == Interface::FITKAbaStepEnum::StateInStep::Deactived || data->getStateInStep(currentStepId) == Interface::FITKAbaStepEnum::StateInStep::ResetToInitial)
            {
                isExistDeactive = true;
                break;
            }
        }
        if (!isExistDeactive)
            op.clear();
        return currentStepId;
    }
}
